package com.dt76.smallloanbackenduser.controller.lhuaw;

import com.dt76.smallloanbackenduser.pojo.lhuaw.StationLetter;
import com.dt76.smallloanbackenduser.remote.lhuaw.StationLettertest;
import com.dt76.smallloanbackenduser.utils.Page;
import com.dt76.smallloanbackenduser.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/lhuaw")
public class StationLetterController {
    @Autowired
    private StationLettertest stationLettertest;

    @RequestMapping("/qryAll")
    public String StationLetter1(Model model){
        model.addAttribute("StationLetterList",stationLettertest.qryAll());
        System.out.println("调用成功");
        System.out.println(stationLettertest.qryAll());
        return "pages/lhuaw/StationMessage";
    }

    @RequestMapping("/getConditionPageAll")
    public String qryStationConditionPageAll(Model model,Integer currentPageNo,
                                             @RequestParam(value ="stationMessageTitle",required = false)String stationMessageTitle,
                                             @RequestParam(value = "stationPublisher",required = false)String stationPublisher
    ){
        if (currentPageNo <= 0) {
            currentPageNo = 1;
        }
        String stationTitle= "";
        String paramstationPublisher="";
        if(stationMessageTitle != null && ! stationMessageTitle.equals("")){
            stationTitle ="%"+stationMessageTitle+"%";
        }
        if(stationPublisher !=null && ! stationPublisher.equals("")){
            paramstationPublisher ="%"+stationPublisher+"%";
        }
        Page page = stationLettertest.qryStationConditionPageAll(currentPageNo,stationTitle,paramstationPublisher);
        model.addAttribute("qryStationLetterList",page.getRows());
        model.addAttribute("page",page);
        model.addAttribute("stationMessageTitle",stationMessageTitle);
        model.addAttribute("stationPublisher",stationPublisher);
        System.out.println(page.getRows());
        return "pages/lhuaw/StationMessage";
    }
    @RequestMapping("/delStation/{stationId}")
    @ResponseBody
    public Result delete(@PathVariable String stationId){
        return stationLettertest.delStation(Integer.parseInt(stationId));
    }

    @RequestMapping("/addStation")
    @ResponseBody
    public Result addStation(
            @RequestParam("stationMessageTitle") String stationMessageTitle,
            String stationId,
            StationLetter stationLetter) {
        Result result = new Result();
        if (stationId.equals("0")) {
            stationLetter.setStationMessageTitle(stationMessageTitle);
            //调用新增接口
            result = stationLettertest.addStation(stationLetter);

            if (result.isSuccess()) {
                //刷新全部的信息
                result.setMsg("添加成功");
            } else {
                result.setMsg("添加失败");
            }
        }
        return result;
    }
    @RequestMapping("/seeStation/{stationId}")
    @ResponseBody
    public StationLetter seeStation(@PathVariable String stationId){
        //调用查询接口
        System.out.println(Integer.parseInt(stationId));
//        System.out.println(stationLettertest.seeStation(Integer.parseInt(stationId)));
        return stationLettertest.seeStation(Integer.parseInt(stationId));
    }
}
